package com.zl.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zl.payment.entity.OverdueBill;
import com.zl.payment.service.IOverdueBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄军鹏
 * @since 2020-07-17
 */
@Controller
@RequestMapping("/payment/overdue-bill")
public class OverdueBillController {
    @Autowired
    private IOverdueBillService overdueBillService;
    
    @GetMapping("overbills/{creditCardId}")
    public String overBills(@PathVariable String creditCardId, Model model){
        List<OverdueBill> overbills = overdueBillService.getOverbills(creditCardId);
        model.addAttribute("overbills",overbills);
        return "view/overbills";
    }
    @GetMapping("overbill/{billId}")
    public String overbill(@PathVariable("billId") String overdueBillId,Model model){
        OverdueBill overdueBill = overdueBillService.getOne(new QueryWrapper<OverdueBill>().eq("OVERDUE_BILL_ID", overdueBillId));
        BigDecimal totalAmount = overdueBillService.totalAmount(overdueBill);
        BigDecimal interest = overdueBillService.interest(overdueBill);
        model.addAttribute("overdueBill",overdueBill);
        model.addAttribute("totalAmount",totalAmount);
        model.addAttribute("interest",interest);
        return "view/overbill";
    }
    @PostMapping("repayOverBill/{billId}")
    public String repayOverBill(@PathVariable("billId")String overdueBillId,BigDecimal totalAmount,String cardNo,Model model){
        OverdueBill overdueBill = overdueBillService.getOne(new QueryWrapper<OverdueBill>().eq("OVERDUE_BILL_ID", overdueBillId));
        boolean b = overdueBillService.repayOverdueBill(overdueBill, cardNo, totalAmount);
        if (b){
            model.addAttribute("message","逾期还款成功");
            return "view/repayResult";
        }
        model.addAttribute("message","逾期还款失败");
        return "view/repayResult";
    }
    
}
